<%@ page language="java" contentType="text/html; charset=UTF-8"
	pageEncoding="UTF-8"%>
<%@ taglib uri="/WEB-INF/tld/c.tld" prefix="c"%>
<%@ taglib uri="/WEB-INF/tld/ui.tld" prefix="ui" %>
<%
	String path = request.getContextPath();
%>
<!DOCTYPE html>
<html lang="zh-CN">
<head>
	<meta http-equiv="X-UA-Compatible" content="IE=edge">
	<meta name="renderer" content="webkit">
	<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
	<title>再融资合同盖章流程附加信息页面</title>
	<jsp:include page="../../import.jsp" flush="true" />
	<style type="text/css">
		.field a { color:#013d79 ;text-decoration:none;}
		.field a:hover { color:#013d79 ;text-decoration:underline;}
		.ellipsis{
		    overflow: hidden;
		    text-overflow: ellipsis;
		    white-space: nowrap;
		 }
	</style>
</head>
<body class="iframeBody">
	<div class="Wrapper">
		<div>
			<div id="pageInTab" class="pageInTab">
				<div tabid="tabA" title="基本信息" >
					<iframe frameborder="0" name="tabA" id="tabA" src=""></iframe>
				</div>
				<div tabid="tabB" title="资产包信息" id="tabBId" style="display: none;">
					<iframe frameborder="0" name="tabB" id="tabB" src=""></iframe>
				</div>
				<div tabid="tabC" title="首付款信息" id="tabCId" style="display: none;">
					<iframe frameborder="0" name="tabC" id="tabC" src=""></iframe>
				</div>
				<div tabid="tabD" title="还款信息" id="tabDId" style="display: none;">
					<iframe frameborder="0" name="tabD" id="tabD" src=""></iframe>
				</div>
				<!-- 初始化时要显示的页签：合同管理 -->
				<div tabid="tabE" title="合同管理" id="tabEId"  lselected="true">
					<form id="fnc_prj_cont_form" >
						<div class="fieldset1 tabel_col clearFix">
							<div class="caption">
								<span class="title">项目基础信息</span>
							</div>
							<div class="fieldBox col_50">
								<div class="label">项目名称</div>
								<div class="field">
									<span id="fncprjnm" name="fncprjnm">&nbsp;${prjScmCr.FNC_PRJ_NM}</span>
								</div>
							</div>
							<div class="fieldBox col_50">
								<div class="label">金融机构</div>
								<div class="field">
									<span id="fncinsNM" name="fncinsNM">&nbsp;${prjScmCr.FUND_RAS_INS_NM}</span>
								</div> 
							</div>
							<div class="fieldBox col_50">
								<div class="label">授信名称</div>
								<div class="field">
									<span id="instypeNM" name="instypeNM">&nbsp; <a href="javascript:showDetailCr(${prjScmCr.CR_ID_KEY});" >${prjScmCr.CR_NM}</a></span>
								</div>
							</div>
							<div class="fieldBox col_50">
								<div class="label">融资报价</div>
								<div class="field">
									<span id="fncscmNM" name="fncscmNM">&nbsp;<a href="javascript:showDetailPrice(${prjScmCr.FNC_SCM_ID});" >${prjScmCr.FNC_SCM_NM}</a></span>
								</div>
							</div>
							<div class="fieldBox col_50">
								<div class="label">业务类型</div>
								<div class="field">
									<span id="bustypeNM" name="bustypeNM">&nbsp;${prjScmCr.BUS_TYPE_}</span>
								</div>
							</div>
							<div class="fieldBox col_50">
								<div class="label">实际融资金额</div>
								<div class="field">
									<span id="acFiAmt" name="acFiAmt"></span>
								</div>
							</div> 
							<div class="fieldBox col_50">
								<div class="label">首付款金额</div>
								<div class="field">
									<span id="firstPayment" name="firstPayment"></span>
								</div>
							</div>
							<div class="fieldBox col_50">
								<div class="label">实际到账金额</div>
								<div class="field">
									<span id="transAm" name="transAm"></span>
								</div>
							</div> 
							<div class="fieldBox col_50">
								<div class="label">到账日期</div>
								<div class="field">
									<span >&nbsp;${prjScmCr.TRANS_TM_}</span>
								</div>
							</div>
							
							<div class="caption">
								<span class="title">到账明细</span>
							</div>
							<div class="fieldBox col_100">
								<div id="cost1" class="sq-grid" />
							</div>
							
							<div class="caption">
								<span class="title">合同明细</span>
							</div>
							<div class="fieldBox col_100">
								<div id="contDetail" class="sq-grid" />
							</div>
							
							<div class="caption">
								<span class="title">合同复印件</span>
							</div>
							<!-- 权证上传合同复印件 -->
							<c:if test="${flag == 'modifyArcRealNum'}">
								<div class="fieldBox col_100">
									<div class="label">合同复印件</div>
									<div class="field">
										<jsp:include page="../frCommFilesUpload.jsp" flush="true" >
											<jsp:param name="filePath" value="ref"/>
										</jsp:include>
										<div id="fileUpload1" class="upfileBoxs"></div>
										<span class="sq-button sq-button-hasicon" id="btnUpload1" onclick="ajaxFileUploadImg('uploads1',1)">
											<i class="sqBtnIco_9"></i>上传
										</span>
									</div>
								</div>
							</c:if>
							<!-- 附件显示 -->
							<div class="fieldBox col_100">
								<div style="width: 98%; margin-left: 1%;">
					            	<jsp:include page="../frCommFilesList.jsp" flush="true" ></jsp:include>
									<div id="ligergrid_file1" class="sq-grid"></div>
								</div>
							</div>
							<div class="caption">
								<span class="title">盖章电子版</span>
							</div>
							<!-- 权证上传盖章电子版 -->
							<c:if test="${flag == 'apply' || flag == 'modifyArcNum' || flag=='pageContract'}"> 
								<div class="fieldBox col_100">
									<div class="label">盖章电子版附件上传</div>
									<div class="field">
										<jsp:include page="../frCommFilesUpload.jsp" flush="true" >
											<jsp:param name="filePath" value="ref"/>
										</jsp:include>
										<div id="fileUpload1" class="upfileBoxs"></div>
										<span class="sq-button sq-button-hasicon" id="btnUpload1" onclick="ajaxFileUploadImg('uploads1',2)">
											<i class="sqBtnIco_9"></i>上传
										</span>
								  </div>
								</div>
							</c:if> 
							<!-- 盖章电子版 -->
							<div class="fieldBox col_100">
								<div style="width: 98%; margin-left: 1%;">
					            	<jsp:include page="../frCommFilesList.jsp" flush="true" ></jsp:include>
									<div id="ligergrid_file2" class="sq-grid"></div>
								</div>
							</div>
							<!-- end 盖章电子版 -->
						</div>
						<!-- 合同主表ID参数 -->
						<div class="fieldBox" style="display:none;">
							<div class="field">
								<input name="mainId" id="mainId" type="text" class="ui-textbox" value="${contId}">
								<input name="flag" id="flag" type="text" class="ui-textbox" value="${flag}">
								<!-- 上传文件使用：系统时间和当前登录人姓名 -->
	                         	<input name="sysDate" id="sysDate" type="text" class="ui-textbox" value="${sysDate }">
	                         	<input name="loginUsrNm" id="loginUsrNm" type="text" class="ui-textbox" value="${loginUsrNm }">
							</div>
						</div>
						<!-- 修改归档份数和实际归档份数 -->
						<c:if test="${flag == 'modifyArcNum' || flag == 'modifyArcRealNum' || flag == 'apply' || flag=='pageContract' }">
						<center>
							<div class="btnsBox">
								<span id="saveContDetailBtn" class="sq-button sq-button-round" onclick="saveContDetail()">保存</span>
								<span id="closeBtn" class="sq-button sq-button-round Hide" onclick="Remove_navTab()">取消</span>
							</div>
						</center>
						</c:if>
					</form>
				</div>
			</div>
		</div>
	</div>
    
	<script type="text/javascript">
		var contDetailGrid;
		var pageInTab = null;
		var loadedTabs=[];
		var pageInApp={
		    		flag:true
		};
		$(function () {
			initTab();
		    $("#acFiAmt").html(fmoney(new  Number("${prjScmCr.AC_FI_AMT}")/100, 2));
		    $("#firstPayment").html(fmoney(new  Number("${prjScmCr.firstPayment}")/100, 2));
		    $("#transAm").html(fmoney(new  Number("${prjScmCr.TRANS_AM}")/100, 2));
		    
			//加载合同复印件列表信息
			var nodeFlag = $("#flag").val();
			if(nodeFlag == "modifyArcRealNum"){
		    	loadFilesLiger("htfyj", 1, "${contId }", "yes");
			}else{
				loadFilesLiger("htfyj", 1, "${contId }", "no");
			}
			//加载盖章电子版
			if(nodeFlag == "apply" || nodeFlag =="modifyArcNum" || nodeFlag =="pageContract"){//发起人确认
		    	loadFilesLiger("gzdzb", 2, "${applyId }", "yes");
			}else{
				loadFilesLiger("gzdzb", 2, "${applyId }", "no");//记得改
			}
			
			//初始化到账明细
			var id="${prjScmCr.ID}";
			if(id != "") {
				$("#cost1").ligerGrid({
		    		url:"<%=path%>/rfbuss/fncprjdrawmon/initList.do?id="+id,
		            method: 'get',
		                columns: [
			                { display: '到账日期', name: 'dmTm', width: 200,format: 'yyyy-MM-dd', editor: { type: 'date'},render:renderDate }, 
			                { display: '到账金额', name: 'dmAm',
			                    editor: { type: 'text' }, align: 'right', width: 150,totalSummary:{
			                        render: function (suminf, column, cell){
			                        	$("#transanttem").val(suminf.sum); 
			                            return '<div>总计:' + suminf.sum.toFixed(2) + '</div>';
			                        }
			                    }
			                },
			                { display: '收款银行', name: 'dueBank',
			                    editor: { type: 'text',readonly:true  }, align: 'center', minWidth: 280
			                },
			                { display: '收款账号', name: 'dmAcc', 
			                    editor: { type: 'text',readonly:true }, align: 'center', width: 180
			                },
			                { display: '账号编号', name: 'accId',hide: true, 
			                    editor: { type: 'text' }, align: 'center', width: 180
			                }			               
		                ],
		                onSelectRow: function (rowdata, rowindex)
		                {
		                },
		                usePager: false,
		                root: 'rows',
		                record: 'total',
		                hideLoadButton: true,
		                width: '100%',
		                height: '100%',
		                heightDiff: -2,
		                enabledEdit: false,
		                isScroll: false,
		                checkbox: true,
		                rownumbers: true,
		                allowUnSelectRow: true,
		                alternatingRow: false,
		                enabledSort: false
		            }); 
			}
			
			//初始化合同明细
			contDetailGrid = $("#contDetail").ligerGrid({
	    		url:"<%=path%>/rfcont/fncPrjContAudit/contDetailApplyList.do?applyId=${applyId}",
	            method: 'get',
	                columns: [
	                    { display: '合同名称', name: 'CONT_DETAIL_NM',width:150},
	                    { display: '主合同编号', name: 'mainContractNo',width:150},
	                    { display: '合同附件', name: 'contDetailAttachList', isEllipsis: false,
	                        render: function (rowdata, rowindex, value) {
	                            var h = '<ul class="gridNanoItems">';
	                            $(value).each(function(index, el) {
	                                h = h + '<li class="ellipsis"><a class="curPointer" href="javascript:downFile(';
	                                h+="'"+el.filePath+"'";
	                                h+=",'"+el.fileNm+"'";
	                                h+=')">'+el.fileNm+'</a></li>';
	                            });
	                            return h + '</ul>';
	                        }
	                    },
	                    { display: '审核状态', name: 'CONT_DETAIL_STATUS_DES' },
	                    { display: '风控审核附件', name: 'contDetailRiskAttachList', isEllipsis: false,
	                        render: function (rowdata, rowindex, value) {
	                            var h = '<ul class="gridNanoItems">';
	                            $(value).each(function(index, el) {
	                            	 h = h + '<li class="ellipsis"><a class="curPointer" href="javascript:downFile(';
	                                 h+="'"+el.filePath+"'";
	                                 h+=",'"+el.fileNm+"'";
	                                 h+=')">'+el.fileNm+'</a></li>';
	                            });
	                            return h + '</ul>';
	                        }
	                    },
	                    { display: '最终版', name: 'prjContDetailFinalList', isEllipsis: false,
	                        render: function (rowdata, rowindex, value) {
	                            var h = '<ul class="gridNanoItems">';
	                            $(value).each(function(index, el) {
	                            	 h = h + '<li class="ellipsis"><a class="curPointer" href="javascript:downFile(';
	                                 h+="'"+el.filePath+"'";
	                                 h+=",'"+el.fileNm+"'";
	                                 h+=')">'+el.fileNm+'</a></li>';
	                            });
	                            return h + '</ul>';
	                        }
	                    },
	                    { display: '风控意见', name: 'prjContOptionList' ,width:180,
	                        	render: function (rowdata, rowindex, value){
	                        		var h = '<ul class="gridNanoItems">';
	                                $(value).each(function(index, el) {
	                                    h = h + '<li class="gridNanoItem2"><div class="ellipsis TL">' + el.CONT_DETAIL_OPINION + '</div>' +
	                                        '<div class="TL info">' + el.CRT_TM_ +' '+el.USR_NM +' '+el.RL_NM + '</div></li>';
	                                });
	                                return h + '</ul>';
	                		}},
	                	getContDetailArcNumCol(),
	                	getContDetailArcRealNumCol(),
	                	getContDetailpaperContractCol(),
	                	{ display: '合同明细ID', name: 'ID', hide: true, align: 'center', width: 100 }
	                ],
	                usePager: false,
	                root: 'rows',
	                record: 'total',
	                width: '100%',
	                fixedCellHeight: false,
	                frozen: false,
	                checkbox:false,
	                rownumbers:true,
	                enabledEdit: true,
	                onAfterShowData: function () {
	                    setNanoScroller();
	                    SetIframe_height ();
	                }
			});
			//contDetailGrid = liger.get("contDetail");
		});
		
		//申请归档份数列
		function getContDetailArcNumCol(){
			var flag = $("#flag").val();
			var data = { display: '申请归档份数', name: 'CONT_DETAIL_ARC_NUM', width: 150, align: 'center', enabledSort: false };
			if(flag == 'modifyArcNum' ||flag =='pageContract'){
				data = { display: '申请归档份数', name: 'CONT_DETAIL_ARC_NUM', width: 150, type: 'int', editor: { type: 'int'} };
			}
			return data;
		}
		//纸质合同归档列
		function getContDetailpaperContractCol(){
			var flag = $("#flag").val();
			var data={ display: '纸质合同归档', name: 'paperContract', width: 180, align: 'center', enabledSort: false,
               render: function (item)
               {
                   if (parseInt(item.paperContract) == 1) return '立即归档';
                   if (parseInt(item.paperContract) == 2) return '后续归档';
               }
         	};
			if(flag == 'modifyArcNum' || flag == 'apply'||flag =='pageContract' ){
				data={ display: '纸质合同归档', name: 'paperContract', width: 180, align: 'center', enabledSort: false,
			       		 editor: { type: 'select', 
									data: [
					                    { value: '立即归档', key: '1' },
					                    { value: '后续归档', key: '2' }
					                ],
									valueField: 'key', textField: 'value' },
		               render: function (item)
		               {
		                   if (parseInt(item.paperContract) == 1) return '立即归档';
		                   if (parseInt(item.paperContract) == 2) return '后续归档';
		               }
			   };
			}
			return data;
		}
		//实际归档份数列
		function getContDetailArcRealNumCol(){
			var flag = $("#flag").val();
			var data = { display: '实际归档份数', name: 'CONT_DETAIL_ARC_REALNUM', width: 150, align: 'center', enabledSort: false};
			if(flag == 'modifyArcRealNum'){
				data = { display: '实际归档份数', name: 'CONT_DETAIL_ARC_REALNUM', width: 150, type: 'int', editor: { type: 'int'} };
			}
			return data;
		}
		
		//控制折行行高
		function setNanoScroller () {
	         $('.gridNanoItems').each(function(index, el) {
	             if ($(el).height() > 100 ) {
	            	 $(el).wrap('<div class="gridNanoContainer"><div class="nano"><div class="nano-content">');
	                 $(el).parents('.gridNanoContainer').height(100);
	                 $(".nano").nanoScroller({
	                     preventPageScrolling: true
	                 });
	             };
	         });
		}
		
		//格式化时间
		function renderDate(rowdata, index, value){
			var dateStr = "";
			if(null != value){
				var date = new Date(value);
				dateStr = DateUtils.formatDate(date, DateUtils.YYYY_MM_DD);
			}
			return dateStr;
		}
		
		//初始化Tab
	    function initTab(){
	    	 pageInTab = $("#pageInTab").ligerTab({
	             height: 'auto',
	             contextmenu: false,
	             minIframeHeight: GET_minIframeHeight(),
	             onBeforeSelectTabItem: function (tabid) {
	             
	             },
	             onAfterSelectTabItem: function(tabid) {
	                     loadedTabs.push(tabid);
	                     switch (tabid){
		                     case "tabA":
		                    	 pageInTab.setTabItemSrc(tabid, "<%=path%>/rfbuss/fncprjbscinfo/toDetailPage2.do?id=${prjScmCr.ID}");
	                         break;
	                         case "tabB":
	                        	 pageInTab.setTabItemSrc(tabid, "<%=path%>/rfbuss/fncprjbscinfo/viewAssPackInfoDetail.do?prjid=${prjScmCr.ID}&flag=2");
	                             break;
	                         case "tabC":
	                        	 pageInTab.setTabItemSrc(tabid, "<%=path%>/rfbuss/fncprjbscinfo/tofstpyDetailPage.do?fncPrjId=${prjScmCr.ID}");
	                             break;  
	                         case "tabD":
	                        	 pageInTab.setTabItemSrc(tabid, "<%=path%>/rfbase/fncprjpaysch/toRepayModifyPage.do?projid=${prjScmCr.ID}");
	                             break;
	                     }
	                 SetIframe_height();
	             }
	         });
	
	         $.extend(true, pageInApp, {
	             pageInTab: pageInTab
	         });
	    };
	    
	    //格式化钱
	    function fmoney(s, n)   
		{   
		   n = n > 0 && n <= 20 ? n : 2;   
		   s = parseFloat((s + "").replace(/[^\d\.-]/g, "")).toFixed(n) + "";   
		   var l = s.split(".")[0].split("").reverse(),   
		   r = s.split(".")[1];   
		   t = "";   
		   for(var i = 0; i < l.length; i ++ )   
		   {   
		      t += l[i] + ((i + 1) % 3 == 0 && (i + 1) != l.length ? "," : "");   
		   }   
		   return t.split("").reverse().join("") + "." + r;   
		} 
	    
	  	//授信详情
		function showDetailCr(id){
			Add_navTab('PrdSpl_3'+id,'授信详情','<%=path%>/rfbase/crbaseinfo/toDetailPage.do?lgcId='+id);
		}
		//报价详情
		function showDetailPrice(id){
			Add_navTab('PrdPrice'+id,'报价详情','<%=path%>/rfbase/scmbaseinfo/toDetail.do?id='+id);
		}
		
		//下载文件方法
		function downFile(_url,_name){
			if(SQ.regs.img.test(_name)){
				SQ.imgPreview(_url, true);
			}else{
				window.open(_url);
			}
		}
		
		//保存合同明细信息和合同复印件
		function saveContDetail(){
            var mainId = $("#mainId").val();
            var nodeFlag = $("#flag").val();
            if(null == mainId){
            	top.$.ligerDialog.SQalert({content: "合同主表ID为空,请联系管理员！",SQ_type: "warning"});
				return false;
            }
            
        	//校验盖章电子版附件是否存在
			var index=2;
			var ligerNm = "ligergrid_file"+index;
			var zui=liger.get(ligerNm);
			var zuiDate=zui.getData();
            
            var jsondata = '{mainId:'+mainId+',docArray:'+JSON.stringify(zuiDate)+',griddata:'+JSON.stringify(contDetailGrid.getData())+'}';
            console.log(jsondata);
			var wait_dialog = null;
			saveFile2('${applyId}','盖章电子版附件操作成功');
			var url = '<%=path%>/rfbuss/fncPrjContMain/saveContDetail.do';
			$.ajax({
				type: 'post',
				url: url,
				contentType: 'application/json',
				dataType: 'json',
				data: JSON.stringify(jsondata),
				beforeSend: function(){
					wait_dialog=$.ligerDialog.SQalert({content: LogUtils.LOG_CMM_DATA_LOADING ,SQ_type: "waitting" });
				},
				success:function(msg){
					wait_dialog.close();
					if(msg.success == 1 && null != msg.data){
						if(nodeFlag == "modifyArcRealNum"){
							saveFile(msg.data,msg.info);//保存文件
						}else{
							top.$.ligerDialog.SQalert({content: msg.info,SQ_type: "success"});
						}
					}else{
						top.$.ligerDialog.SQalert({content: msg.info,SQ_type: "error"});
					}
				}
			});
        }
		
		//显示已上传但是未保存的文件列表
		function showFiles(data, index){
			var sysDate = $("#sysDate").val();
			var loginUsrNm = $("#loginUsrNm").val();
			var f_Grid = liger.get("ligergrid_file"+index);
			var uploadJOSN = data;
    		for (var i=0; i< uploadJOSN.length; i++){ 
        		//添加行-追加行
        		f_Grid.addRow({fileNm: uploadJOSN[i].fileNm, crtUsrRlNm: loginUsrNm, crtTm: sysDate, doSthFlag: "1", ligerIndex: index, id: i});
    		}
		}
		
		//保存文件
		function saveFile(pkId,info){
			var docArray = JSON.stringify(getUploadResultParams(1));
	        var url = "<%=path%>/rfbuss/fncPrjContDetail/uploadAttchment.do";
        	if(null == docArray){
        		//无要上传的文件-直接结束
        		top.$.ligerDialog.SQalert({content: info,SQ_type: "success"});
        	}else{
		        if(pkId){//拖车主表ID不为空执行
			        //赋值附件
			        var data = {
						"docArray":docArray,
						"pkId": pkId,
						"flag": "htfyj"
					};
					//请求
			        AjaxUtils.ajaxPost(url, data, function(msg){
			        	if(msg.success == 1){
							 top.$.ligerDialog.SQalert({content: info,SQ_type: "success"});
							 if(msg.data){
								 //刷新附件页面GRID方法和清空上传按钮
								 refreshUlFileRecList(1);
								 clearUploadResultParams(1);
							 }
						 }else{
							 UIUtils.alertError(msg.info);
						 }
			        }, true);
		        }else{
		        	$.ligerDialog.SQalert({content: "业务主键为空-不能保存文件！",SQ_type: "error"});
		        }
        	}
		}
		//保存文件
		function saveFile2(pkId,info){
			var docArray = JSON.stringify(getUploadResultParams(2));
	        var url = "<%=path%>/rfbuss/fncPrjContDetail/uploadAttchment.do";
	        if(null == docArray || docArray == '[]'  ){
	        	//无要上传的文件-直接结束
        		//top.$.ligerDialog.SQalert({content: "",SQ_type: "success"});
        	}else{
		        if(pkId){//拖车主表ID不为空执行
			        //赋值附件
			        var data = {
						"docArray":docArray,
						"pkId": pkId,
						"flag": "gzdzb"
					};
					//请求
			        AjaxUtils.ajaxPost(url, data, function(msg){
			        	if(msg.success == 1){
							 top.$.ligerDialog.SQalert({content: info,SQ_type: "success"});
							 if(msg.data){
								 //刷新附件页面GRID方法和清空上传按钮
								 refreshUlFileRecList(2);
								 clearUploadResultParams(2);
							 }
						 }else{
							 UIUtils.alertError(msg.info);
						 }
			        }, true);
		        }else{
		        	$.ligerDialog.SQalert({content: "业务主键为空-不能保存文件！",SQ_type: "error"});
		        }
        	}
		}
	</script>
</body>
</html>